<template>
  <section class="file-manage">
    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>文件管理</span>
        <span class="tip">* 只保留最近上传的20个文件信息</span>
      </section>
      <el-button style="margin-left: 10px" @click="handleCreate">上传文件</el-button>
      <section class="main-buttons-btns" />
    </section>
    <section class="url-table">
      <v-table :list-loading="loading" :selection="false" :filter-column="filtercolumn" tag="fileManage" :list="dataList" style="width: 100%">
        <el-table-column
          fixed="right"
          label="操作"
          width="120"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="copyUrl(scope.row,'oss')"
            >
              复制oss链接
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="copyUrl(scope.row,'document')"
            >
              复制本地链接
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
            <el-button
              v-if="scope.row.typeCode!=='KCLX04'"
              type="text"
              size="small"
              @click="handleView(scope.row)"
            >
              预览
            </el-button>
          </template>
        </el-table-column>
      </v-table>
    </section>
    <uploadFile v-if="isShowFileDialog" @close="handleClose" @onSubmint="handleSubmit" />
    <el-dialog
      title="预览"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <section class="content">
        <template v-if="fileInfo.typeCode==='KCLX02'">
          <el-image style="width:300px;" :src="fileInfo.ossPath" :preview-src-list="[fileInfo.ossPath]" />
        </template>
        <template v-else>
          <video width="300px" controls="controls" :src="fileInfo.ossPath" />
        </template>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </section>
</template>

<script>
import VTable from '@/components/Table'
import uploadFile from '../components/uploadFile'
import { queryFileList, deleteFile, addFile } from '../api'
export default {
  name: 'FileManage',
  components: {
    uploadFile,
    VTable
  },
  data() {
    return {
      loading: false,
      dataList: [],
      isOss: sessionStorage.getItem('FILE_SAVE_THIRD_CONFIG') || false,
      isShowFileDialog: false,
      dialogVisible: false,
      fileInfo: {},
      view: '/API-DOCUMENT/document/file/download?id='
    }
  },
  created() {
    this.getList()
  },
  mounted() {
  },
  methods: {
    /**
     * 复制地址
     */
    copyUrl(row, type) {
      const ths = this
      this.$copyText(this.getUrl(row, type)).then(function(e) {
        ths.$message.success('复制成功')
        console.log(e)
      }, function(e) {
        ths.$message.error('复制失败，请手动复制')
        console.log(e)
      })
    },
    /**
     * 获取图片地址
     */
    getUrl(row, tpye) {
      if (tpye === 'oss') {
        return row.ossPath
      } else {
        return this.view + row.id
      }
    },
    formattingUrl(row) {
      if (this.isOss) {
        return row.ossPath
      } else {
        return this.view + row.id
      }
    },
    handleCreate() {
      this.isShowFileDialog = true
    },
    handleClose() {
      this.isShowFileDialog = false
    },
    handleSubmit(fileInfo) {
      const request = JSON.stringify(fileInfo)
      addFile({ file: request }).then(res => {
        if (res.data.code === '200') {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.handleClose()
          this.getList()
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: '请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.3)'
        })
        deleteFile({ id: row.id }).then(res => {
          loading.close()
          this.$message({
            type: 'success',
            message: res.data.msg || '操作成功'
          })
          this.getList()
        }).catch(err => {
          loading.close(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getList() {
      this.loading = true
      queryFileList().then(res => {
        this.loading = false
        if (res.data.code === '200') {
          this.dataList = res.data.data
        }
      }).catch(() => {
        this.loading = false
      })
    },
    handleView(row) {
      this.fileInfo = Object.assign({}, row)
      this.dialogVisible = true
    },
    filtercolumn(scope, item) {
      if (item === 'id') {
        return scope.$index + 1
      } else {
        return scope.row[item]
      }
    }

  }
}
</script>

<style lang="scss">
.file-manage{
    padding:10px;
    .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
      .tip{
        color:red;
        font-size: 18px;
        margin-left:15px;
      }
    }
    }
    .url-table{
        margin-top: 10px;
    }
    .content{
      display: flex;
        justify-content: center;
        align-items:center;
        width:100%;
        padding: 35px;
    }
    .dialog-footer{
      display: flex;
      justify-content: center;
      align-content: center;
    }
}
</style>
